/*
 * Main.java
 *
 * Created on April 8, 2008, 8:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

//package p3t1;

/**
 *
 * @author Student
 */
public class Main extends Applet {//implements Runnable{

    /** Creates a new instance of Main */
	private ChatWindow cw;
	private MessageWindow mw;
	private LoginWindow log;
	private JTextArea message;
	private JTextArea chat;
	private String s;
	private ChatDB db = new ChatDB();
	private int session;
	//private Thread runner;
	private String user = "Scott";

    /**
     * @param args the command line arguments
     */
     public void init()
     {
       // TODO code application logic here
       setLayout (new BorderLayout());
	   cw = new ChatWindow();
	   mw = new MessageWindow();
	   log = new LoginWindow();

	   add (cw, BorderLayout.CENTER);
	   add (mw, BorderLayout.SOUTH);
	   add (log, BorderLayout.NORTH);
	   //cw.setVisible(false);
	   //mw.setVisible(false);

	   setPreferredSize (new Dimension(500,500));
	   //setBackground (Color.yellow);
	//	runner = new Thread(this);
	//	runner.start();
    }

	/*public void run(){
		try{
		while(true) {
			ResultSet rs = db.getMessage();
			chat.setText("");
			while(rs.next())
			{
				chat.append(rs.getString(2) + ": " + rs.getString(3) + "\n");
			}
			repaint();
			try
			{
				runner.sleep(50);
			}
			catch( InterruptedException e ){}


		}
		}
		catch (Exception e ){}

	}*/

	class LoginWindow extends JPanel
	{
		private JButton login;
		private	JTextField username;
		LoginWindow()
		{
			login = new JButton("Login");
			ButtonListener listener = new ButtonListener();

			login.addActionListener (listener);
			username = new JTextField(10);
			add (username);
			add (login);
			//setPreferredSize (new Dimension(500,500));
		}
		 private class ButtonListener implements ActionListener
		 {
				      public void actionPerformed (ActionEvent event)
				      {
				        if (event.getSource() == login)
				        {
						  user = username.getText();
						 // cw.setVisible(true);
	 					  //mw.setVisible(true);
						  //log.setVisible(false);

				        }
				      }
    	 }


	}

    class ChatWindow extends JPanel
   	{


		ChatWindow()
		{
			chat = new JTextArea(10,30);

			chat.setLineWrap(true);
			JScrollPane scrollingChat = new JScrollPane(chat);

			add (scrollingChat);
			setPreferredSize (new Dimension(50, 200));

		}

	}

	class MessageWindow extends JPanel
	{

		private JButton msgBtn;

		MessageWindow()
		{

			message = new JTextArea(3,30);
			message.setLineWrap(true);


			msgBtn = new JButton ("Send");

			ButtonListener listener = new ButtonListener();

			msgBtn.addActionListener (listener);
			JScrollPane scrollingMessage = new JScrollPane(message);

			add (scrollingMessage);
			add (msgBtn);

			setPreferredSize (new Dimension(200,150));


		}


		 private class ButtonListener implements ActionListener
		  {
		      public void actionPerformed (ActionEvent event)
		      {
		        if (event.getSource() == msgBtn)
		        {



				  s = message.getText();
				  db.sendMessage(user,s);
				  //s = s + "\n";
				  message.setText("");

				  chat.append(user + ": " + s + "\n");

		        }
		      }
    	   }

		}


}
